package com.jakesepicstuff.jnotepad;

import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class SaveFile {
	
	JFileChooser fileChooser = new JFileChooser();
	
	// CONSTRUCTOR
	public SaveFile() {
		
		int returnVal = fileChooser.showSaveDialog(null);
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			
			File fileToSave = fileChooser.getSelectedFile();
			
			saveTheFile(fileToSave);
		}
	}
	
	// SAVE FILE
	public void saveTheFile(File fileToSave) {
		
		
		FileWriter fw;
		BufferedWriter bw;
		
		try {				
			fw = new FileWriter(fileToSave);
			bw = new BufferedWriter(fw);
			
			bw.write(Display.textArea.getText());
			bw.close();
			JOptionPane.showMessageDialog(null, fileToSave + " has been saved");
			System.out.println(fileToSave + " has been saved");	
			
			} catch (IOException e) {
				
				JOptionPane.showMessageDialog(null, "Error saving file!");
				e.printStackTrace();
		}
	}
}